package com.szgr.baoyin.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.szgr.baoyin.MyApplication;
import com.szgr.baoyin.R;
import com.szgr.baoyin.adapter.MyProtectListAdapter;
import com.szgr.baoyin.model.MyProtectItem;
import com.szgr.baoyin.model.ProtectState;
import com.szgr.baoyin.utils.Constant;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MyProtectActivity extends BaseActivity {

    private List<MyProtectItem> data;
    private Handler mHandler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what){
                case 23:
                    top.setVisibility(View.GONE);
                    protectListView.setVisibility(View.GONE);
                    center.setVisibility(View.VISIBLE);
                    break;
                case 24:
                    top.setVisibility(View.VISIBLE);
                    protectListView.setVisibility(View.VISIBLE);
                    center.setVisibility(View.GONE);
//                    listAdapter.setData(data);
                    break;
                case 25:
                    for (ProtectState ps : states) {
                        for (MyProtectItem mpi : data) {
                            if(ps.getSerialno().equals(mpi.getSerialno())){
                                mpi.setHasBuy(true);
                                mpi.setMaintenance_id(ps.getMaintenance_id());
                            }
                        }
                    }
                    listAdapter.setData(data);
                    break;
                case 26:
                    listAdapter.setData(data);
                    break;
            }
        }
    };
    private MyProtectListAdapter listAdapter;
    private View top;
    private View center;
    private ListView protectListView;
    private List<ProtectState> states;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_my_protect);
        super.onCreate(savedInstanceState);
    }

    @Override
    public void initActionBar() {
        actionBarTitle.setText("我的维保");
    }

    @Override
    public void initView() {
        top = findViewById(R.id.my_protect_top);
        center = findViewById(R.id.my_protect_center);
        findViewById(R.id.cart_go_store).setOnClickListener(this);

        data = new ArrayList<>();
        protectListView = (ListView) findViewById(R.id.protect_list_view);
        listAdapter = new MyProtectListAdapter(this, data, this);
        protectListView.setAdapter(listAdapter);

        getMyProtect();

        getMyBuyProtect();
    }

    private void getMyBuyProtect() {
        StringRequest sr = new StringRequest(Request.Method.POST, Constant.URL_HASBUY_PROTECT,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String s) {
                        System.out.println("---name--->" + s);
                        try {
                            JSONObject jsonObject = new JSONObject(s);
                            JSONObject datas = jsonObject.getJSONObject("datas");
                            JSONArray maintenanceList = datas.getJSONArray("$maintenance_list");
                            Gson gson = new Gson();
                            states = gson.fromJson(maintenanceList.toString(),new TypeToken<List<ProtectState>>(){}.getType());
                            if(states != null && states.size() == 0){
                                mHandler.obtainMessage(26).sendToTarget();
                            }else {
                                mHandler.obtainMessage(25).sendToTarget();
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {

            }
        }){
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String,String> map = new HashMap<>();
                map.put("key",MyApplication.getmInstance().getKey());
                return map;
            }
        };
        MyApplication.getmInstance().addToRequestQueue(sr);
    }

    private void getMyProtect() {
        StringRequest sr = new StringRequest(Request.Method.POST, Constant.URL_MY_PROTECT,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String s) {
                        System.out.println("------>" + s);
                        try {
                            JSONObject jsonObject = new JSONObject(s);
                            JSONObject datas = jsonObject.getJSONObject("datas");
                            JSONArray order_maintenance_list = datas.getJSONArray("$order_maintenance_list");
                            Gson gson = new Gson();
                            data = gson.fromJson(order_maintenance_list.toString(),new TypeToken<List<MyProtectItem>>(){}.getType());
                            if(data != null && data.size() == 0){
                                mHandler.sendEmptyMessage(23);
                            }else{
                                mHandler.sendEmptyMessage(24);
                            }
                        } catch (JSONException e) {
                            mHandler.sendEmptyMessage(23);
                            e.printStackTrace();
                        }
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {

            }
        }){
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String,String> map = new HashMap<>();
                map.put("key",MyApplication.getmInstance().getKey());
                return map;
            }
        };
        MyApplication.getmInstance().addToRequestQueue(sr);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.action_bar_left:
                finish();
                break;
            case R.id.protect_apply:
                int tag = (int) v.getTag();
                MyProtectItem myProtectItem = data.get(tag);
                if(myProtectItem.isHasBuy()) {
                    Intent intent = new Intent(this, ProtectDetailActivity.class);
                    intent.putExtra("maintenance_id",myProtectItem.getMaintenance_id());
                    startActivity(intent);
                }else{
                    startActivity(new Intent(this, ProtectActivity.class));
                }
                break;
            case R.id.cart_go_store:
                startActivity(new Intent(this,BuyProtectActivity.class));
                break;
        }
    }
}
